#! /usr/bin/env python
# -*- coding: utf-8 -*-


import pygame

from position import *
from gameUtils import *

class DebugSprite(pygame.sprite.Sprite):
	hidden_img=None
	def __init__(self,parent,floorName,fscale_Xmax):
		u"""Dirt Class"""
		pygame.sprite.Sprite.__init__(self, self.containers) # Sprite inicialization
		
		self.parent=parent.parent
		self.floor=parent
		
		self.surface=load_transparency(floorName+'.png')
		xmax=self.surface.get_size()[0]
		self.scale=float(xmax)*fscale_Xmax
		for x in range(self.surface.get_size()[0]):
			for y in range(self.surface.get_size()[1]):
				self.clean_px((x,y))
	
	def getLarguraPx(self):
		return self.surface.get_size()[0]
	
	def getAlturaPx(self):
		return self.surface.get_size()[1]
		
	def getLargura(self):
		return self.getLarguraPx()*self.scale
	
	def getAltura(self):
		return self.getAlturaPx()*self.scale
	
	
	def isClean_px(self,(x,y)):
		return self.surface.get_at((x,y))[3] == 0
	
	def clean(self,pos):
		ipos=(pos.x,self.floor.getAltura()-pos.y)
		self.clean_px(resize(ipos,self.scale))
		
	def clean_area(self,pos,area):
		ipos=(pos.x,self.floor.getAltura()-pos.y)
		x,y=resize(ipos,self.scale)
		
		for dx,dy in area.points:
			self.clean_px((x+dx,y+dy))
	
	def clean_px(self,(x,y)):
		self.surface.set_at((x,y),(0,0,0,0))
		
	def paint(self,pos,color=(255,0,0,255)):
		ipos=(pos.x,self.floor.getAltura()-pos.y)
		self.paint_px(resize(ipos,self.scale),color)
	
	def paint_px(self,(x,y),color=(255,0,0,255)):
		self.surface.set_at((x,y),color)
		
	def sum(self):
		value=0
		
		for x in range(self.surface.get_width()):
			for y in range(self.surface.get_height()):
				value+=self.surface.get_at((x,y))[1]
		
		return value
	
	def drawPositions(self,positions):
		for pos in positions:
			self.paint(pos,(255,0,255,255))
	
	def mean(self):
		return self.sum()/self.surface.get_width()/self.surface.get_height()
		
	def update(self,visible):
		if visible:
			self.image=pygame.transform.smoothscale(self.surface, self.parent.screen.get_size())
		else:
			self.image=self.hidden_img
			
		self.rect = self.image.get_rect()
		#~ aux=self.rect.inflate(200, 200).fit(pygame.Rect(0, 0, 400, 400))
		#~ self.rect=aux


